﻿using BrotliSharpLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

public class EncryptHelper
{
    public static string Encode(string str)
    {
        var bytes = System.Text.Encoding.UTF8.GetBytes(str);
        using (var memoryStream = new MemoryStream())
        {
            using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Compress, true))
            {
                brotliStream.Write(bytes, 0, bytes.Length);
            }

            var result = Convert.ToBase64String(memoryStream.ToArray());
            return result;
        }
    }

    public static string DeEncode(string str)
    {
        var bytes = Convert.FromBase64String(str);
        using (var memoryStream = new MemoryStream(bytes))
        {
            using (var outputStream = new MemoryStream())
            {
                using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
                {
                    decompressStream.CopyTo(outputStream);
                }
                var buffer = outputStream.ToArray();
                return System.Text.Encoding.UTF8.GetString(buffer);
            }
        }
    }

    public static void CompressFile(string inputFilePath, string outputFilePath)
    {
        byte[] fileData = File.ReadAllBytes(inputFilePath);
        using (var memoryStream = new MemoryStream())
        {
            using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Compress, true))
            {
                brotliStream.Write(fileData, 0, fileData.Length);
            }

            var buffer = memoryStream.ToArray();
            File.WriteAllBytes(outputFilePath, buffer);
        }
    }

    public static void DecompressFile(string inputFilePath, string outputFilePath)
    {
        byte[] compressedData = File.ReadAllBytes(inputFilePath);
        using (var memoryStream = new MemoryStream(compressedData))
        {
            using (var outputStream = new MemoryStream())
            {
                using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
                {
                    decompressStream.CopyTo(outputStream);
                }
                var buffer = outputStream.ToArray();
                File.WriteAllBytes(outputFilePath, buffer);
            }
        }
    }

    //public byte[] CompressJson(string jsonString)
    //{
    //    byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);
    //    using (MemoryStream memoryStream = new MemoryStream())
    //    {
    //        using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
    //        {
    //            gzipStream.Write(jsonBytes, 0, jsonBytes.Length);
    //        }
    //        return memoryStream.ToArray();
    //    }
    //}

    public static string CompressString(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        using (var memoryStream = new MemoryStream())
        {
            using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gzipStream.Write(buffer, 0, buffer.Length);
            }
            return Convert.ToBase64String(memoryStream.ToArray());
        }
    }

    public static string DecompressString(string compressedText)
    {
        byte[] gzipBuffer = Convert.FromBase64String(compressedText);
        using (var memoryStream = new MemoryStream(gzipBuffer))
        {
            using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
            {
                using (var reader = new StreamReader(gzipStream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }

    static void DownloadFile(string url, string localPath)
    {
        try
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(url, localPath);
                Console.WriteLine("文件已成功下载到： " + localPath);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("下载文件时发生错误： " + ex.Message);
        }
    }

}
